package com.cnzz.site1255174697.factory;

import android.content.Context;
import android.text.TextUtils;

import com.cnzz.site1255174697.ConstHost;
import com.cnzz.site1255174697.model.BannerEntity;
import com.cnzz.site1255174697.model.BaseEntity;
import com.cnzz.site1255174697.model.BigEntity;
import com.cnzz.site1255174697.model.GoodsTagEntity;
import com.cnzz.site1255174697.model.KindsTopEntity;
import com.cnzz.site1255174697.model.LogoEntity;
import com.cnzz.site1255174697.model.MallListEntity;
import com.cqyanyu.yanyu.http.XApi;
import com.cqyanyu.yanyu.http.XCallback;
import com.cqyanyu.yanyu.http.XResult;
import com.cqyanyu.yanyu.http.XResultList;
import com.cqyanyu.yanyu.http.XResultPage;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;

/**
 * 数据工厂类
 */

public class IndexFactory {

    /**
     * 分类标题--分类下面的子分类
     */
    public static void getIndexTop(Context context, String pid, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.INDEX_TOP_NAME);
        requestParams.addBodyParameter("pid", pid);
        XApi.get(context, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultList xResultList = XResultList.fromJson(result, BaseEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 获取首页轮播
     *
     * @param context
     * @param callback
     */
    public static void getBanner(Context context, String type, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.INDEX_BANNER);
        if (!TextUtils.isEmpty(type)) {
            requestParams.addBodyParameter("type", type);
        }
        XApi.get(context, requestParams, new Callback.CacheCallback<String>() {
            @Override
            public boolean onCache(String result) {
                XResultList xResultList = XResultList.fromJson(result, BannerEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList.data);
                }
                return false;
            }
            @Override
            public void onSuccess(String result) {
                XResultList xResultList = XResultList.fromJson(result, BannerEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList.data);
                }
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }
            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 非常大牌
     *
     * @param context
     * @param callback
     */
    public static void getSuperBig(final Context context, int page, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.SUPER_BIG_URL);
        requestParams.addBodyParameter("page", page + "");
        XApi.get(context, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultPage xResultList = XResultPage.fromJson(result, BigEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 获取商品标签
     *
     * @param context
     * @param callback
     */
    public static void getGoodsTag(final Context context, String classId, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.GOODS_TAG_URL);
        requestParams.addBodyParameter("classId ", classId);
        XApi.get(context, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultList xResultList = XResultList.fromJson(result, GoodsTagEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList.data);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 商品列表
     *
     * @param context
     * @param classId  分类Id
     * @param tagId    标签Id  (不传代表分类全部)
     * @param type     商品类型1天猫，2淘宝  (不传代表全部)
     * @param callback
     */
    public static void getGoodsList(final Context context, String classId, String tagId, String type, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.GOODS_LIST_URL);
        requestParams.addBodyParameter("classId", classId);
        if (!TextUtils.isEmpty(tagId)) {
            requestParams.addBodyParameter("tagId", tagId);
        }
        if (!TextUtils.isEmpty(type)) {
            requestParams.addBodyParameter("type", type);
        }
        XApi.get(context, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultList xResultList = XResultList.fromJson(result, MallListEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList.data);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 分类最顶部
     *
     * @param context
     * @param callback
     */
    public static void getTopType(final Context context, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.KINDS_ALONE_URL);
        XApi.get(context, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultList xResultList = XResultList.fromJson(result, KindsTopEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });

    }

    /**
     * 获取首页Logo图标
     *
     * @param context
     * @param callback
     */
    public static void getLogo(final Context context, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.LOGO_URL);
        XApi.get(context, requestParams, new Callback.CacheCallback<String>() {
            @Override
            public boolean onCache(String result) {
                XResult xResult = XResult.fromJson(result, LogoEntity.class);
                if (xResult.code == 0) {
                    callback.onSuccess(xResult.data);
                }
                return false;
            }

            @Override
            public void onSuccess(String result) {
                XResult xResult = XResult.fromJson(result, LogoEntity.class);
                if (xResult.code == 0) {
                    callback.onSuccess(xResult.data);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 获取标签
     *
     * @param context
     * @param callback
     */
    public static void getTag(Context context, final XCallback callback) {
        RequestParams requestParams = new RequestParams(ConstHost.TAG_URL);
        XApi.get(context, requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                XResultList xResultList = XResultList.fromJson(result, BaseEntity.class);
                if (xResultList.code == 0) {
                    callback.onSuccess(xResultList);
                }
            }
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });

    }
}
